package model.chat;

import java.io.IOException;
import java.util.HashMap;

import org.jboss.netty.channel.Channel;

import protocol.chat.Chat_protocol;

import config.ChatConfig;

import model.message.MessageEncoder;
import model.message.MessageVo;
import send.Sender;
import util.KIntChange;

public class ChatServer {
	private static ChatServer	chatServer;
	
	public static ChatServer getInstance() {
		if (chatServer == null) {
			chatServer = new ChatServer();
		}
		return chatServer;
	}
	
	public void addMsg(ChatVo msg) {
		
		 
		int operate = msg.getOperate();
		try {
			if (operate == ChatConfig.getInstance().getOnly()) {
//				onlyChat(map);
			} else if (operate == ChatConfig.getInstance().getWorld()) {
				
				chatBroadCast(msg);
				
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 私聊
	 * */
	public void onlyChat(HashMap msg) {
		
	}
	
	public void chatBroadCast(ChatVo msg) throws IOException {
		
//		String msgString = MessageEncoder.JSONString(msg);
		Chat_protocol chat_protocol=new Chat_protocol();
		chat_protocol.setContent(msg.get_content());
		chat_protocol.setFrom_user_id(msg.get_from_user_id());
		chat_protocol.setTo_user_id(msg.get_to_user_id());
		chat_protocol.setOperate(msg.getOperate());
		msg.getChannel().write(chat_protocol.write());
	}
	
	public void chatOnly() {
		
	}
	// /end class
}
